/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dao;

import com.entity.Classes;
import com.entity.Student;
import com.entity.Teacher;
import com.opensymphony.xwork2.ActionContext;
import com.sun.org.apache.xml.internal.security.encryption.Transforms;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

/**
 *
 * @author DELL
 */
public class Usermanager {

    private Session session;
    private Transaction transaction;
    private Query query;

    private SessionFactory sessionFactory = null;

    private HibernateTemplate ht = null;

    public void setSessionFactory(SessionFactory sessionFactory) {

        this.sessionFactory = sessionFactory;
    }

    private HibernateTemplate getHibernateTemplate() {
        if (ht == null) {

            ht = new HibernateTemplate(sessionFactory);
        }
        return ht;
    }

    public void saveStudent(Student st) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        try {

            transaction = session.beginTransaction();
            session.save(st);
            transaction.commit();

        } catch (Exception e) {
        }
        session.close();
    }

    public void updateStudent(Student st) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        try {
   Logger.getLogger(this.getClass().getName()).log(Level.INFO, "1111222222222222222222222111");
            transaction = session.beginTransaction();
            session.saveOrUpdate(st);
            transaction.commit();

        } catch (Exception e) {
        }
        session.close();
    }

    
    
    
      public void updatePassword(Student st) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        
        String hql="update Student s set s.password=?  where s.sno=?";
        
        
        
        
        try {
   
            transaction = session.beginTransaction();
            
            Query query=session.createQuery(hql);
           
             
            query.setString(0, st.getPassword());
             query.setString(1, st.getSno());
             
             
             query.executeUpdate();
             
            Logger.getLogger(this.getClass().getName()).log(Level.INFO, "1111333333344444444444444444444444444444444444411");
            transaction.commit();

        } catch (Exception e) {
        }
        session.close();
    }  
    
    
       public void updateStudent1(Student st) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        
        String hql="update Student s set s.userName=? ,sex=? ,department=? ,jiguan=? ,email=? ,tel=?    where s.sno=?";
        
        
        
        
        try {
   Logger.getLogger(this.getClass().getName()).log(Level.INFO, "1111222222222222222222222111");
            transaction = session.beginTransaction();
            
            Query query=session.createQuery(hql);
             Logger.getLogger(this.getClass().getName()).log(Level.INFO, "111122233333333333333333333333333333333333333333333222111");
             
            query.setString(0, st.getUserName());
             query.setString(1, st.getSex());
             query.setString(2, st.getDepartment());
             query.setString(3, st.getJiguan());
             query.setString(4, st.getEmail());
             query.setString(5, st.getTel());
              query.setString(6, st.getSno());
             
             query.executeUpdate();
             
            Logger.getLogger(this.getClass().getName()).log(Level.INFO, "1111333333344444444444444444444444444444444444411");
            transaction.commit();

        } catch (Exception e) {
        }
        session.close();
    }  
    
    
    
    
    
    
    
    public void deleteStudent(String sno) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        try {

            transaction = session.beginTransaction();
            session.delete(sno);
            transaction.commit();

        } catch (Exception e) {
        }
        session.close();
    }

    public Student getStudent(int stId) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();
        Student student = (Student) session.get(Student.class, stId);
        session.close();
        return student;
    }

    public List<Student> allStudent(int pageNumber) {
        List<Student> allStudents = new ArrayList<Student>();
        String hqlString = "from Student as st";

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();

        query = session.createQuery(hqlString);
        query.setFirstResult((pageNumber - 1) * 10);
        query.setMaxResults(10);
        allStudents = query.list();
        session.close();
        return allStudents;

    }

    public List<Classes> allClasses(int pageNumber) {
        List<Classes> allClasses = new ArrayList<Classes>();
        String hqlString = "from Classes as cs";
        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();

        query = session.createQuery(hqlString);
        query.setFirstResult((pageNumber - 1) * 10);
        query.setMaxResults(10);
        allClasses = query.list();
        session.close();
        return allClasses;

    }

    public Student getStudent1(String m) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();

 
        
        Query query2 = (Query) session.createQuery("from Student as s where sno='" + m + "'");
        
        
       
        
        Student st = (Student) query2.uniqueResult();
        session.close();
        return st;

    }

    public boolean stuLogin(Student stu) {

        if (stu.getSno() != null && stu.getPassword() != null) {
            String hqlString = "from Student as s where sno='" + stu.getSno() + "' and password='" + stu.getPassword() + "'";
            Student st = null;
            ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
            SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
            Session session = sessionFactory.openSession();
            Query query = (Query) session.createQuery(hqlString);
            st = (Student) query.uniqueResult();

            Logger.getLogger(this.getClass().getName()).log(Level.INFO, "ttttttttt4444444444444444444444444443331");

            if (st != null) {
                Logger.getLogger(this.getClass().getName()).log(Level.INFO, "ttttttttt4444444444444444444444444443331");
                session.close();
                return true;
            }

        }
        return false;

        //            session = sessionFactory.openSession();
//             Logger.getLogger(this.getClass().getName()).log(Level.INFO, "tttttttttttttttttttttttt1111112222222222222221");
//            
//            Query query = (Query) session.createQuery("from Student as s where sno='" + stu.getSno() + "' and password='" + stu.getPassword() + "'");
//            
//             Logger.getLogger(this.getClass().getName()).log(Level.INFO, "tttttttttttttttttttttttt1111112222222222222221");
//            Student st = (Student) query.uniqueResult();
//             Logger.getLogger(this.getClass().getName()).log(Level.INFO, "tttttttttttttttttttttttt111133333333333331"); 
    }

    public Student getStudent2(int stId) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();

        Query query2 = (Query) session.createQuery("from Student as s where stId='" + stId + "'");

        Student st = (Student) query2.uniqueResult();
        session.close();
        return st;
    }

    public int getStudentAmount() {
        int studentAmount = 0;

        ApplicationContext ctx = new FileSystemXmlApplicationContext("jwMSys/applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");       //创建sessionFactory对象
        Session session = sessionFactory.openSession();

        String hqlString = "select count(*) from Student as st";
        query = session.createQuery(hqlString);
        long count = (Long) query.uniqueResult();
        studentAmount = (int) count;

        session.close();
        return studentAmount;
    }

}
